/*
 * 2520 is the smallest number that can be divided by each of the numbers
 * from 1 to 10 without any remainder.
 * 
 * What is the smallest positive number that is evenly divisible by
 * all of the numbers from 1 to 20?
 */

package pj1;

import java.util.ArrayList;
import java.util.HashMap;

class Problem5 {
	
	static final int problem = 20;
	
	// power function
	private static Integer exponential(Integer base, Integer power) {
		Integer result = 1;
		for (int i = 0; i < power; i++) {
			result *= base;
		}
		return result;
	}

	// Java's shallow clone will not work...
	private static ArrayList<Integer> deepCopy(ArrayList<Integer> a) {
		ArrayList<Integer> clone = new ArrayList<Integer>();
		for (Integer i : a) {
			clone.add(i);
		}
		return clone;
	}

	public static void main(String[] args) {
		ArrayList<Integer> factors = new ArrayList<Integer>();
		HashMap<Integer, ArrayList<Integer>> factoredNums = new HashMap<Integer, ArrayList<Integer>>();
		HashMap<Integer, Integer> count = new HashMap<Integer, Integer>();
		HashMap<Integer, Integer> previous = new HashMap<Integer, Integer>();
		Integer c = 0;
		Integer result = 1;
		int p;

		// begin finding factors
		for (int n=2; n<=problem; n++) {
			p = n;

			// find prime factors
			for (int i=2; i <= p; i++) {
				while (p%i == 0) {
					factors.add(i);
					p /= i;
				}
			}
			factoredNums.put(n, deepCopy(factors));
			factors.clear();
		}

		// initialize Hashmaps
		for (int n=2; n<=problem; n++) {
			previous.put(n, 0);
			count.put(n, 0);
		}

		// count the maximum occurrences of each prime number
		for (Integer i : factoredNums.keySet()) {
			
			// count occurrences of each factor of the number
			for (Integer n : factoredNums.get(i)) {
				c = previous.get(n).intValue();
				c++;
				previous.put(n, c);
			}
			
			// keep the maximum occurrences of each prime number
			for (Integer m : count.keySet()) {
				if (previous.get(m).intValue() > count.get(m).intValue()) {
					count.put(m, previous.get(m).intValue());
				}
				previous.put(m, 0);
			}
		}
		
		// multiply the counted result
		for (Integer k : count.keySet()) {
			result *= exponential(k, count.get(k).intValue());
		}
		
		System.out.println(result);
	}
}
